Skip to content

fix(W1019): pass the whole Fn::Sub value to child rules - #4964

Merged
kddejong merged 1 commit into
aws-cloudformation:mainfrom
Varshith-Kali:fix-4959-w1019-unused-sub-vars
Nov 20, 2026
Merged

kddejong merged 1 commit into
aws-cloudformation:mainfrom
Varshith-Kali:fix-4959-w1019-unused-sub-vars

Conversation

@Varshith-Kali

Copy link
Copy Markdown
Contributor

What broke

W1019 (unused Fn::Sub variables) no longer reports anything since 1.26.0. A template with a variable map key that the string never uses produces no finding at all.

Root cause

In src/cfnlint/rules/functions/Sub.py, fn_sub rebinds value = value[0] for the two-element form and then calls each child rule with rule.validate(value_validator, s, value, schema). W1019 therefore receives only the string, and SubParametersUsed.validate returns early on a string (if validator.is_type(instance, "string"): return), so the variable map is never checked. The child call passed instance.get("Fn::Sub") (the whole pair) until #3962, first released in 1.26.0. W1020 still fired because it already handles both the string and pair forms.

Fix

  • Sub.fn_sub again passes the whole Fn::Sub value to its child rules, restoring the pre-Add rule W1051 to validate if dyn ref when arn #3962 behavior. W1019 sees the variable map again; W1020 is unchanged in behavior.
  • DynamicReferenceSecretsManagerArn (W1051, also a child rule of Sub) now handles the two-element form by checking its string element, so restoring the pair cannot break it.

Reproduction

Template (from the issue): two AWS::SSM::Parameter resources using !Sub with a two-element form where map key B is never used in the string.

Before (upstream main at 008ead690):

W1020 'Fn::Sub' isn't needed because there are no variables
template.yaml:16:7

After (this branch):

W1019 Parameter 'B' not used in 'Fn::Sub'
template.yaml:8:7

W1019 Parameter 'B' not used in 'Fn::Sub'
template.yaml:16:7

W1020 'Fn::Sub' isn't needed because there are no variables
template.yaml:16:7

This matches the 1.25.1 output quoted in the issue, including the W1020 finding still firing.

Tests

  • New regression tests in test/unit/rules/functions/test_sub.py (test_sub_child_rules_get_whole_sub_value, 3 cases): unused variable in the two-element form, unused variable with no variable used at all (W1019 plus W1020), and all variables used (no findings). Verified the two W1019 cases fail with the fix reverted and pass with it.
  • pytest test/unit/rules/functions/test_sub.py test/unit/rules/functions/test_sub_parameters_used.py test/unit/rules/functions/test_sub_unneeded.py test/unit/rules/functions/test_sub_needed.py test/unit/rules/functions/test_sub_not_join.py: 54 passed.
  • pytest test/unit/rules/functions/: 296 passed.
  • pytest test/unit/rules/: 2185 passed.
  • ruff check on all three changed files: clean.

Fixes #4959

@Varshith-Kali

Copy link
Copy Markdown
Contributor Author

Hi maintainers, this PR is ready for review. The CI workflows are waiting on first-time-contributor approval, so a workflow run approval would be appreciated when you get a chance. Thanks.

Since 1.26.0, Sub.fn_sub rebound value to the string element of the
two-element form before calling its child rules, so SubParametersUsed
(W1019) received only a string and returned early without ever
checking the variable map. Restore the whole Fn::Sub value for the
child rule calls, as was the case before aws-cloudformation#3962, and make the W1051
child rule handle the two-element form by checking its string
element. W1020 already handled both forms and is unchanged in
behavior.

Fixes aws-cloudformation#4959

Signed-off-by: Varshith Puli <pulivarshit@gmail.com>
@kddejong
kddejong force-pushed the fix-4959-w1019-unused-sub-vars branch from 7129aa5 to f19adb1 Compare November 20, 2026 18:51
@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.82%. Comparing base (720c5cc) to head (f19adb1).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4964   +/-   ##
=======================================
  Coverage   94.82%   94.82%           
=======================================
  Files         441      441           
  Lines       16185    16187    +2     
  Branches     3138     3139    +1     
=======================================
+ Hits        15348    15350    +2     
  Misses        463      463           
  Partials      374      374           
Flag Coverage Δ
unittests 94.82% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kddejong
kddejong merged commit c063cf6 into aws-cloudformation:main Nov 20, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

W1019: unused Fn::Sub variables are no longer reported since 1.26.0

2 participants